package com.mengchengnet.chaperonage.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.mengchengnet.chaperonage.BaseActivity;
import com.mengchengnet.chaperonage.R;
import com.mengchengnet.chaperonage.util.PreferenceUtil;
import com.mengchengnet.chaperonage.util.Url;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/23.
 */

public class SelectAddressActivity extends BaseActivity {
    private Button btn_sure;
    private EditText ed_name,ed_phone,ed_address,ed_relationship,ed_others;
    private RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selectaddress);
        toolbarState=TOOLBAR_STYLE_BACK_TITLE;
        initToolbar("编辑资料");
        initView();

    }

    private void initView() {
        requestQueue= Volley.newRequestQueue(getApplication());
        ed_name= (EditText) findViewById(R.id.selectAddress_ed_name);
        ed_phone= (EditText) findViewById(R.id.selectAddress_ed_phone);
        ed_address= (EditText) findViewById(R.id.selectAddress_ed_address);
        ed_relationship= (EditText) findViewById(R.id.selectAddress_ed_relationship);
        ed_others= (EditText) findViewById(R.id.selectAddress_ed_others);
        btn_sure= (Button) findViewById(R.id.selectAddress_btn_sure);
        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!ed_name.getText().toString().isEmpty()){
                    if (!ed_relationship.getText().toString().isEmpty()){
                        if(!ed_address.getText().toString().isEmpty()){
                            if (!ed_phone.getText().toString().isEmpty()){
                                upLoad();
                                Intent intent =new Intent();
                                intent.putExtra("name",ed_name.getText().toString());
                                intent.putExtra("relationship",ed_relationship.getText().toString());
                                intent.putExtra("address",ed_address.getText().toString());
                                intent.putExtra("phone",ed_phone.getText().toString());
                                intent.putExtra("others",ed_others.getText().toString());
                                SelectAddressActivity.this.setResult(RESULT_OK,intent);
                                SelectAddressActivity.this.finish();

                            }else {
                                Toast.makeText(SelectAddressActivity.this, "电话不能为空", Toast.LENGTH_SHORT).show();
                            }

                        }
                        else {
                            Toast.makeText(SelectAddressActivity.this, "地址不能为空", Toast.LENGTH_SHORT).show();
                        }
                    }
                    else {
                        Toast.makeText(SelectAddressActivity.this, "关系不能为空", Toast.LENGTH_SHORT).show();
                    }

                }else {
                    Toast.makeText(SelectAddressActivity.this, "姓名不能为空", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }



    private void upLoad(){
        StringRequest request = new StringRequest(Request.Method.POST, Url.uploadUserInfo,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String result) {
                        Log.e("选择陪护", "选择陪护接口返回值："+result );
                        JSONObject jsonObject;
                        try {
                            if(result != null && result.startsWith("\ufeff"))
                            {
                                result =  result.substring(1);
                            }
                            jsonObject = new JSONObject(result);
                            if (jsonObject.getString("code").equals("00")){
                                Toast.makeText(SelectAddressActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                                //finish();
                            }
                            else {
                                Toast.makeText(SelectAddressActivity.this, "失败", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {
                Toast.makeText(SelectAddressActivity.this, "网络异常请稍后重试！", Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("version", PreferenceUtil.getString(getApplication(),"versionCode"));
                map.put("loginId", PreferenceUtil.getString(getApplication(),"userId"));
                map.put("name", ed_name.getText().toString());
                map.put("relationShip", ed_relationship.getText().toString());
                map.put("address", ed_address.getText().toString());
                map.put("phone", ed_phone.getText().toString());
                map.put("remarks", ed_others.getText().toString());
                return map;
            }
        };
        request.setTag("GetFenZu");
        request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, // 默认超时时间，应设置一个稍微大点儿的
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, // 默认最大尝试次数
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        requestQueue.add(request);

    }
}
